//
//  UIViewController+common.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/10.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "UIViewController+common.h"
#import "GRRootNavigationController.h"
#import "GRLoginViewController.h"

@implementation UIViewController (common)

-(void)presentViewController:(UIViewController *)viewController completion:(void (^)(void))completion {
    if (viewController) {
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"backIndicator"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(dismissModalViewControllerAnimatedYes)];
          UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
        
        [self presentViewController:nav animated:YES completion:completion];
    }
}

- (void)dismissModalViewControllerAnimatedYes{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)presentViewController:(UIViewController *)viewController navigationbarColor:(UIColor *)color {
    if (viewController) {
        GRRootNavigationController *navi = [[GRRootNavigationController alloc] initWithRootViewController:viewController navigationBarColor:color];
        
        [self presentViewController:navi animated:YES completion:nil];
    }
}

- (void)goToLogin {
    GRLoginViewController *login = [[GRLoginViewController alloc] init];
    [self presentViewController:login completion:nil];
}

- (void)haveLoginOperation:(void(^)(void))completion {
    if ([GRUserBasisInfo isLogin]) {
        completion();
    }else {
        [self goToLogin];
    }
}

+ (void)displacementLogin {
    UIViewController *aimVc = [self currentViewController];
    
    if (![aimVc isKindOfClass:[GRLoginViewController class]]) {
        GRLoginViewController *login = [[GRLoginViewController alloc] init];
        [aimVc presentViewController:login completion:nil];
    }
}


- (void)dismissALLViewController {
    UIViewController *vc = self;
    
    while (vc.presentingViewController) {
        vc = vc.presentingViewController;
    }
    [vc dismissViewControllerAnimated:YES completion:nil];
}

+ (UIViewController *)currentViewController {
    
    UIViewController *currentVc = nil;
    UIViewController *appRootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVc = appRootVc;
    
    if (topVc.presentedViewController) {
        topVc = topVc.presentedViewController;
        NSLog(@"数组:%@", topVc.childViewControllers);
        NSLog(@"模态出最外层的视图%@", [topVc.childViewControllers lastObject]);
        //有模态情况下的根视图
        currentVc = [topVc.childViewControllers lastObject];
    } else {
        //获取非模态情况下的根视图
        currentVc = [self currentNavi];
    }
    
    return currentVc;
}

+ (UIViewController *)currentNavi {
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        result = nextResponder;
    } else {
        result = window.rootViewController;
    }
    /*
     *  在此判断返回的视图是不是你的根视图--我的根视图是UITabBarController
     */
    if ([result isKindOfClass:[UITabBarController class]]) {
        UITabBarController *mainTabBarVC = (UITabBarController *)result;
        result = [mainTabBarVC selectedViewController];
        result = [result.childViewControllers lastObject];
    }
    
    NSLog(@"非模态视图%@", result);
    return result;
}


- (void)setNavigationBarBottomLineHidden:(BOOL)hidden {
    if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
        NSArray *list=self.navigationController.navigationBar.subviews;
        for (id obj in list) {
            if ([obj isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=(UIImageView *)obj;
                NSArray *list2=imageView.subviews;
                for (id obj2 in list2) {
                    if ([obj2 isKindOfClass:[UIImageView class]]) {
                        UIImageView *imageView2=(UIImageView *)obj2;
                        imageView2.hidden=hidden;
                    }
                }
            }
        }
    }
}

- (void)setBackBarButtonItemWithImage:(UIImage *)image {
    UIImage *newImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)];
    UIBarButtonItem*backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"";
    [backItem setBackButtonBackgroundImage:newImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    self.navigationItem.backBarButtonItem = backItem;
}


-(void)constrainViewEqual:(UIView *) view
{
    [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    NSLayoutConstraint *conCenterX = [NSLayoutConstraint constraintWithItem:view
                                                                  attribute:NSLayoutAttributeCenterX
                                                                  relatedBy:0
                                                                     toItem:self.view
                                                                  attribute:NSLayoutAttributeCenterX
                                                                 multiplier:1
                                                                   constant:0];
    
    NSLayoutConstraint *conCenterY = [NSLayoutConstraint constraintWithItem:view
                                                                  attribute:NSLayoutAttributeCenterY
                                                                  relatedBy:0
                                                                     toItem:self.view
                                                                  attribute:NSLayoutAttributeCenterY
                                                                 multiplier:1
                                                                   constant:0];
    
    NSLayoutConstraint *conWidth = [NSLayoutConstraint constraintWithItem:view
                                                                attribute:NSLayoutAttributeWidth
                                                                relatedBy:0
                                                                   toItem:self.view
                                                                attribute:NSLayoutAttributeWidth
                                                               multiplier:1
                                                                 constant:0];
    
    NSLayoutConstraint *conHeight = [NSLayoutConstraint constraintWithItem:view
                                                                 attribute:NSLayoutAttributeHeight
                                                                 relatedBy:0
                                                                    toItem:self.view
                                                                 attribute:NSLayoutAttributeHeight
                                                                multiplier:1
                                                                  constant:0];
    
    NSArray *constraints = @[conCenterX,conCenterY,conWidth,conHeight];
    [self.view addConstraints:constraints];
}

@end
